/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 4, 2012  11:39:16 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : MmLivcontractorOutbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.MmGoodsreceiptOutb;
import jpa.entitys.synchro.staging.MmLivcontractorDeduct;
import jpa.entitys.synchro.staging.MmLivcontractorOutb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class MmLivcontractorOutbServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(MmLivcontractorOutbServices.class);

	/**
	 * @param em
	 */
	public MmLivcontractorOutbServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public List<MmLivcontractorOutb> findByDocumentNo(String company, String project, String documentNo, String status, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("documentNo", documentNo);
		mapWhereValue.put("status", status);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmLivcontractorOutb.findByDocumentNo", mapWhereValue));				
	}

	public MmLivcontractorOutb findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "MmLivcontractorOutb.findById"));				
	}	
	
	public List<MmLivcontractorOutb> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("MmLivcontractorOutb.findAllIsSync", mapWhereValue));				
	}
	
	public List<MmLivcontractorOutb> findByIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		mapWhereValue.put("isactive", "Y");

		return super.ConvertToYoureObject(super.find("MmLivcontractorOutb.findByIsSync", mapWhereValue) );
	}	
	
	public List<MmLivcontractorOutb> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "MmLivcontractorOutb.findByInstanceNo"));				
	}
	
	public MmLivcontractorOutb UpdatedIsSyncYes(MmLivcontractorOutb value) throws Exception
	{
		if(value != null)
		{		
			value.setIssync("Y");
			return (MmLivcontractorOutb)super.updated(value);
		}else
			return null;
	}
	
	public MmLivcontractorOutb Updated(MmLivcontractorOutb value) throws Exception
	{
		if(value != null)
		{		
			return (MmLivcontractorOutb)super.updated(value);
		}else
			return null;
	}
	
	public MmLivcontractorOutb InsertedOrUpdated(MmLivcontractorOutb vlaue) throws Exception {
		MmLivcontractorOutb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("MM_LIVCONTRACTOR_OUTB");					
				obj.setPromisStagingId(id);
				obj.setCreatedBy(this.getUser());
				obj.setUpdatedBy(this.getUser());				
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}

	public List<MmLivcontractorOutb> Inserted(List<MmLivcontractorOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmLivcontractorOutb obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("MM_LIVCONTRACTOR_OUTB");					
					obj.setPromisStagingId(id);
					obj.setCreatedBy(this.getUser());
					obj.setUpdatedBy(this.getUser());				
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}	
			}
			
			super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<MmLivcontractorOutb> Inserted(List<MmLivcontractorOutb> vlauelist, List<MmLivcontractorDeduct> lstPoDeuct) throws Exception {
		try{
			
			
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			if(CommonService.getSize(vlauelist) > 0){
				for(MmLivcontractorOutb obj : vlauelist)
				{
					if(CommonService.IsNull(obj.getPromisStagingId()))
					{
						String id = super.SequenceNext("MM_LIVCONTRACTOR_OUTB");					
						obj.setPromisStagingId(id);
						obj.setCreatedBy(this.getUser());
						obj.setUpdatedBy(this.getUser());				
						obj.setSyncCreatedby(this.getUser());
						obj.setSyncCreatedon(this.getCurrent_date());
						obj.setSyncUpdatedby(this.getUser());
						obj.setSyncUpdatedon(this.getCurrent_date());
						obj.setIsactive("Y");
						obj.setIssync("N");
						common_lst.add(obj);
					}	
				}
			}
			
			List<CommonEntity> common_lst1 = new ArrayList<CommonEntity>();
			if(CommonService.getSize(lstPoDeuct) > 0){
				for(MmLivcontractorDeduct obj : lstPoDeuct)
				{
					String id = super.SequenceNext("MM_LIVCONTRACTOR_DEDUCT");					
					obj.setPromisStagingId(id);
					obj.setCreatedBy(this.getUser());
					obj.setUpdatedBy(this.getUser());				
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");	
					common_lst1.add(obj);
				}							
			}
			
			
			EntityTransaction t = null;
			try{			
		        t = this.getEm().getTransaction();	        	        
		        t.begin();
		        
		        if(CommonService.getSize(common_lst) > 0){
		        	for(CommonEntity obj : common_lst){
			        	this.getEm().persist(obj); 
			        }			        	
		        }		
		        
		        if(CommonService.getSize(common_lst1) > 0){
	        		for(CommonEntity obj : common_lst1){
			        	this.getEm().persist(obj);
			        }
	        	}
		        
		        t.commit();
		        
			}finally{
				if (t.isActive()) 
					  t.rollback();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	
	public List<MmLivcontractorOutb> Updated(List<MmLivcontractorOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmLivcontractorOutb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	
	public List<MmLivcontractorDeduct> UpdatedDeduct(List<MmLivcontractorDeduct> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(MmLivcontractorDeduct obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}

}
